package com.example.a22310.myapplication.Model;

import com.example.a22310.myapplication.Beans.Post;
import com.example.a22310.myapplication.Beans.UserInfo;
import com.example.a22310.myapplication.Beans.user;
import com.example.a22310.myapplication.Listener.ClubListener;
import com.example.a22310.myapplication.Listener.PostListener;
import com.example.a22310.myapplication.Listener.UserListener;
import com.example.a22310.myapplication.Service.service;
import com.example.a22310.myapplication.ViewModel.User.AlterInformationViewModel;
import com.example.a22310.myapplication.ViewModel.User.GetUserInformationViewModel;
import com.example.a22310.myapplication.ViewModel.User.UserInformationViewModel;

import okhttp3.MultipartBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static android.provider.ContactsContract.CommonDataKinds.Website.URL;

public class UserModel {

    private Retrofit retrofit;
    public UserModel()
    {
        retrofit= new Retrofit.Builder()
                .baseUrl(URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    public void login(UserModel user, final UserListener listener) {  //登录
        service userService = retrofit.create(service.class);
        Call<UserModel> call = userService.userLogin(user.getUser_user(), user.getUser_password());
        System.out.println("---"+user.getUser_user());
        System.out.println("---"+user.getUser_password());
        Callback<UserModel> callback = new Callback<UserModel>() {
            @Override
            public void onResponse(Call<UserModel> call, Response<UserModel> response) {
                UserModel user = response.body();
                listener.onSuccess(user);
            }
            @Override
            public void onFailure(Call<UserModel> call, Throwable t) {
                listener.onFailure();
            }
        };
        call.enqueue(callback);
    }

    public void register(UserModel user, final UserListener listener) {  //注册
        service userService = retrofit.create(service.class);
        Call<UserModel> call = userService.register(user.getUser_user(),user.getUser_username(), user.getUser_password());
        Callback<UserModel> callback = new Callback<UserModel>() {
            @Override
            public void onResponse(Call<UserModel> call, Response<UserModel> response) {
                UserModel user = response.body();
                listener.onSuccess(user);
            }
            @Override
            public void onFailure(Call<UserModel> call, Throwable t) {
                listener.onFailure();
            }
        };
        call.enqueue(callback);
    }

    public void acquireUserPic(UserModel user, final UserListener listener) {  //获取用户头像
        service userService = retrofit.create(service.class);
        Call<UserModel> call = userService.userPhoto(user.getUser_user());
        Callback<UserModel> callback = new Callback<UserModel>() {
            @Override
            public void onResponse(Call<UserModel> call, Response<UserModel> response) {
                UserModel user = response.body();
                listener.onSuccess(user);
            }
            @Override
            public void onFailure(Call<UserModel> call, Throwable t) {
                listener.onFailure();
            }
        };
        call.enqueue(callback);
    }
    /*
     *获取验证码，
     * 检测手机号在数据库是否存在
     */
    public void isPhoneExsit(int flag, UserModel user, final UserListener listener) {
        service userService = retrofit.create(service.class);
        Call<UserModel> call = userService.Register(flag,user.getUser_user());
        Callback<UserModel> callback = new Callback<UserModel>() {
            @Override
            public void onResponse(Call<UserModel> call, Response<UserModel> response) {
                UserModel user = response.body();
                listener.onSuccess(user);
            }
            @Override
            public void onFailure(Call<UserModel> call, Throwable t) {
                listener.onFailure();
            }
        };
        call.enqueue(callback);
    }

    /*
     *检测验证码是否正确
     */
    public void checkVerify(UserModel user, final UserListener listener) {
        service userService = retrofit.create(service.class);
        Call<UserModel> call = userService.checkSMSCode(user.getUser_user(),user.getVerify());
        Callback<UserModel> callback = new Callback<UserModel>() {
            @Override
            public void onResponse(Call<UserModel> call, Response<UserModel> response) {
                UserModel user = response.body();
                listener.onSuccess(user);
            }
            @Override
            public void onFailure(Call<UserModel> call, Throwable t) {
                listener.onFailure();
            }
        };
        call.enqueue(callback);
    }

    /*
     *重置密码
     **/
    public void resetPwd(UserModel user, final UserListener listener) {
        service userService = retrofit.create(service.class);
        Call<UserModel> call = userService.resetPassword(user.getUser_user(),user.getVerify(),user.getUser_password());
        Callback<UserModel> callback = new Callback<UserModel>() {
            @Override
            public void onResponse(Call<UserModel> call, Response<UserModel> response) {
                UserModel user = response.body();
                listener.onSuccess(user);
            }
            @Override
            public void onFailure(Call<UserModel> call, Throwable t) {
                listener.onFailure();
            }
        };
        call.enqueue(callback);
    }




    public void postList(final PostListener<Post> listener, int userid, int page) {  //用户帖子列表
        service userService = retrofit.create(service.class);
        Call<Post> call = userService.userpostlist(userid,21,page);
        Callback<Post> callback = new Callback<Post>() {
            @Override
            public void onResponse(Call<Post> call, Response<Post> response) {
                Post post = response.body();
                listener.onSuccess(post);
            }
            @Override
            public void onFailure(Call<Post> call, Throwable t) {
                listener.onFailure();
            }
        };
        call.enqueue(callback);
    }



    public void alterUser(final AlterInformationViewModel listener, int userid, String username, String address, MultipartBody.Part file) {  //修改用户信息
        service userService = retrofit.create(service.class);
        Call<user> call = userService.alteruser(userid,username,address,file);
        Callback<user> callback = new Callback<user>() {
            @Override
            public void onResponse(Call<user> call, Response<user> response) {
                user user = response.body();
                listener.onSuccess(user);
            }



            @Override
            public void onFailure(Call<user> call, Throwable t) {
                listener.onFailure();


            }
        };
        call.enqueue(callback);
    }

    public void newsNumber(final ClubListener<user> listener, int userid) {   //首页消息气泡数
        service userService = retrofit.create(service.class);
        Call<user> call = userService.newsBubbleNumber(userid);
        Callback<user> callback = new Callback<user>() {
            @Override
            public void onResponse(Call<user> call, Response<user> response) {
                user number = response.body();
                listener.onSuccess(number);
            }



            @Override
            public void onFailure(Call<user> call, Throwable t) {
                listener.onFailure();
            }
        };
        call.enqueue(callback);
    }

    public void userInfo(final GetUserInformationViewModel listener, int userid) {   //首页消息气泡数
        service userService = retrofit.create(service.class);
        Call<UserInfo> call = userService.userInf(userid);
        Callback<UserInfo> callback = new Callback<UserInfo>() {
            @Override
            public void onResponse(Call<UserInfo> call, Response<UserInfo> response) {
                UserInfo userInfo = response.body();
                listener.onSuccess(userInfo);
            }
            @Override
            public void onFailure(Call<UserInfo> call, Throwable t) {
                listener.onFailure();
            }
        };
        call.enqueue(callback);
    }




}
